/** 电话号码的字母组合
 * @param {string} digits
 * @return {string[]}
 */
/* var letterCombinations = function(digits) { 
    let num2Str = [0,0,"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
    let digitsArr = digits.split("").map(item=>{
        return parseInt(item)
    })
    let code = []  //将数字数组转换为字符串格式的数组 [2,3,4] => ["abc", "def", "ghi"]
    digitsArr.forEach(item=>{
        if(!!num2Str[item]){
            code.push(num2Str[item])
        }
    })
    let combine =(arr)=>{
        let tmp = []
        if(arr.length>1){
           for(let i=0;i<arr[0].length;i++){
                for(let j=0;j<arr[1].length;j++){
                    tmp.push(`${arr[0][i]}${arr[1][j]}`)
                }
            } 
        }else if(arr.length==1){
            for(let i=0;i<arr[0].length;i++){
                tmp.push(`${arr[0][i]}`)
            }
        }
        
        // 非常重要的替换删除操作
        arr.splice(0,2,tmp)
        if(arr.length>1){
            combine(arr)
        }else{
            return tmp
        }
        return arr[0]
    }
    return combine(code)
}; */

var letterCombinations = function (digits) {
  // 建立数字与字母的对应关系
  let letter = [0, 0, "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
  let digitNum = digits.split("").map((item) => parseInt(item, 10)); //2
  let showLetter = [];
  for (let i = 0; i < digitNum.length; i++) {
    // console.log(letter[digitNum[i]]); // digitNum [ 2,3 ]
    showLetter.push(letter[digitNum[i]]);
  }
//   console.log(showLetter);
  return showEveryLetter(showLetter);
};
let combine = (arr) => {
  let tmp = [];
  if (arr.length > 1) {
    for (let i = 0; i < arr[0].length; i++) {
      for (let j = 0; j < arr[1].length; j++) {
        tmp.push(`${arr[0][i]}${arr[1][j]}`);
      }
    }
  } else if (arr.length == 1) {
    for (let i = 0; i < arr[0].length; i++) {
      tmp.push(`${arr[0][i]}`);
    }
  }

  // 非常重要的替换删除操作
  arr.splice(0, 2, tmp);
  if (arr.length > 1) {
    combine(arr);
  } else {
    return tmp;
  }
  return arr[0];
};
function showEveryLetter(letter) {
  if (letter.length === 0) return [];
  if (letter.length === 1) {
    return letter[0].split("");
  }
  let combineTwo = [];
  if (letter.length > 1) {
    for (let i = 0; i < letter[0].length; i++) {
      for (let j = 0; j < letter[1].length; j++) {
        combineTwo.push(letter[0][i] + letter[1][j]);
      }
    }
  }
  letter.splice(0, 2, combineTwo);
//   console.log(letter, combineTwo, "outter");
  if (letter.length > 1) {
    // console.log(letter, "before");
    showEveryLetter(letter);
    // 大于2个参数时的返回值
    // console.log(letter, "letter");
    return letter[0];
  } else {
    return combineTwo;
  }
}
// console.log(showEveryLetter([["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"],'ghi']));

console.log(letterCombinations("234"));
